package net.ithe.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.ithe.mapper.StudentMapper;
import net.ithe.pojo.PageResult;
import net.ithe.pojo.Student;
import net.ithe.pojo.StudentQueryParam;
import net.ithe.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        //1.设置分页参数(PageHelper--工具带的类)
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        //2.执行查询
        List<Student> studentList = studentMapper.list(studentQueryParam);
        //3.封装
        Page<Student> p=(Page<Student>)studentList;
        return new PageResult<Student>(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        Student student = studentMapper.getById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void updateViolation(Short id, Short score) {
        Student student =getById(Integer.valueOf(id));
        student.setViolationCount((short)(student.getViolationCount() + 1));
        student.setViolationScore((short) (student.getViolationScore() + score));
        studentMapper.updateViolation(student);
    }

}
